package cn.edu.scau.cmi.zengGuanSheng.springAndhibernate.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import cn.edu.scau.cmi.zengGuanSheng.springAndhibernate.domain.Teacher;
import cn.edu.scau.cmi.zengGuanSheng.springAndhibernate.util.HibernateSessionFactoryUtil;

public class TeacherDao {
	
	//添加教师
	public void addTeacher(Teacher t) {
		Session s = HibernateSessionFactoryUtil.getSession();
		s.save(t);
		HibernateSessionFactoryUtil.commitTransactionAndClose(s);
	}
	
	//列出所有教师信息
	public List<Teacher> listTeacher() {
		Session s = HibernateSessionFactoryUtil.getSession();
		Query q =s.createQuery("from Teacher t");
		List<Teacher> listOfTeacher = q.list();
		HibernateSessionFactoryUtil.commitTransactionAndClose(s);
		return listOfTeacher;
	}
	
	//根据教师姓名查找教师
	public Teacher findTeacherByName(Teacher t) {
		Session s = HibernateSessionFactoryUtil.getSession();
		Query q =s.createQuery("from Teacher t where t.name = ?");
		q.setString(0, t.getName());
        List<Teacher> list= q.list();
        HibernateSessionFactoryUtil.commitTransactionAndClose(s);
        return list.get(0);
	}
	
	//删除教师
	public void deleteTeacher(Teacher t) {
		//没有这个业务需求，暂不实现
	}
	
	//修改教师
	public void editTeacher(Teacher t) {
		//没有这个业务需求，暂不实现
	}

}
